package io.mft.datasource.db.accessor.impl;

import java.util.List;

import io.mft.commons.db.SQL;
import io.mft.commons.db.mapper.SQLExecutorDbMapper;
import io.mft.commons.db.mapper.TableDbMapper;
import io.mft.commons.exception.MFTException;
import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.commons.function.response.kv.KVMapList;
import io.mft.datasource.db.accessor.DataAccessor;
import io.mft.datasource.db.accessor.bean.DAContext;
import io.mft.datasource.db.accessor.bean.DAResponse;
import io.mft.datasource.db.accessor.bean.DbContextValue;
import io.mft.datasource.db.accessor.sql.SQLScript;
import io.mft.datasource.db.accessor.sql.SQLScriptManager;
import io.mft.datasource.db.accessor.util.ProcedureFunctionSQLParameterTypeBuilder;

/**
 * 数据库普通查询操作
 * @说明：TODO
 * @类型名称：DbQueryAccessor
 * @创建者: 胡常建
 * @创建时间: 2021年1月28日 下午5:05:33
 * @修改者: 胡常建
 * @修改时间: 2021年1月28日 下午5:05:33
 */
public class DbQueryAccessor extends DataAccessor<DbContextValue> {

	@Override
	public ExecuteReturn doExecute(DAContext<DbContextValue> context) {
		try {
			DbContextValue dbContextValue = context.getContextValue();
			SQLScript sqlScript = SQLScriptManager.getInstance().get(dbContextValue.getScriptKey());
			DAResponse daResponse = context.getResponse();
			switch (sqlScript.getType()) {
			case table:
				TableDbMapper<KVMap> mapper=TableDbMapper.getInstance(dbContextValue.getDatasourceId(), sqlScript.getScript(), KVMap.class);
				KVMap where=new KVMap();
				where.putAll(context.getRequest().getRequestValue());
				List<KVMap> list=mapper.find(where, true);
				KVMapList kvMapList=new KVMapList(list);
				daResponse.result(kvMapList);
				break;
			case sql:
				SQL sql = SQLScriptManager.getInstance().loadSQL(dbContextValue.getScriptKey(), context.getRequest().getRequestValue());
				SQLExecutorDbMapper<KVMap> sqlExecutorDbMapper=SQLExecutorDbMapper.getInstance(dbContextValue.getDatasourceId(), KVMap.class);
				List<KVMap> list1=sqlExecutorDbMapper.findBySql(sql.getSql(), sql.getArgs());
				KVMapList kvMapList1=new KVMapList(list1);
				daResponse.result(kvMapList1);
				break;
			case sqlfunction:
				SQL sqlfunction = SQLScriptManager.getInstance().loadSQL(dbContextValue.getScriptKey(), context.getRequest().getRequestValue());
				String[] fparamTypes=ProcedureFunctionSQLParameterTypeBuilder.build(sqlfunction, context.getRequest().getRequestValue());
				SQLExecutorDbMapper<KVMap> sqlfunctionExecutorDbMapper=SQLExecutorDbMapper.getInstance(dbContextValue.getDatasourceId(), KVMap.class);
				List<KVMap> sqlfunctionList=sqlfunctionExecutorDbMapper.executeFunction(sqlfunction.getSql(),fparamTypes, sqlfunction.getArgs());
				KVMapList sqlfunctionkvMapList=new KVMapList(sqlfunctionList);
				daResponse.result(sqlfunctionkvMapList);
				break;
			case sqlprocedure:
				SQL sqlprocedure = SQLScriptManager.getInstance().loadSQL(dbContextValue.getScriptKey(), context.getRequest().getRequestValue());
				String[] pparamTypes=ProcedureFunctionSQLParameterTypeBuilder.build(sqlprocedure, context.getRequest().getRequestValue());
				SQLExecutorDbMapper<KVMap> sqlprocedureExecutorDbMapper=SQLExecutorDbMapper.getInstance(dbContextValue.getDatasourceId(), KVMap.class);
				List<KVMap> sqlprocedureList=sqlprocedureExecutorDbMapper.executeFunction(sqlprocedure.getSql(),pparamTypes, sqlprocedure.getArgs());
				KVMapList sqlprocedurekvMapList=new KVMapList(sqlprocedureList);
				daResponse.result(sqlprocedurekvMapList);	
				break;
			default:
				throw new MFTException(-1, "不支持的SQLScript类型");
			}
			ExecuteReturn executeReturn=ExecuteReturn.builder().build();
			return executeReturn; 
		} catch (Exception e) {
			throw new MFTException(e);
		}
	}

}
